$(function () {
    //1.抽取变量
    var customer_form = $("#customer_form");
    var customer_datagrid = $("#customer_datagrid");
    var customer_dialog = $("#customer_dialog");
    var transfer_dialog = $("#transfer_dialog");
    var transfer_form = $("#transfer_form");
    var lost_form = $("#lost_form");
    var lost_dialog = $("#lost_dialog");

    var methodObj = {

        add: function () {
            //清空表单数据
            customer_form.form("clear");

            customer_dialog.dialog("setTitle", "新增");

            customer_dialog.dialog("open");
        },
        edit: function () {
            //判断选中数据
            var row = customer_datagrid.datagrid("getSelected");
            if (!row) {
                $.messager.alert('温馨提示', '请选中一条数据！', 'info');
                return;
            }

            if (row.job) {
                row["job.name"] = row.job.name;
            }
            if (row.source) {
                row["source.name"] = row.source.name;
            }
            if (row.salaryLevel) {
                row["salaryLevel.name"] = row.salaryLevel.name;
            }
            //回显表单
            customer_form.form("load", row);

            customer_dialog.dialog("setTitle", "编辑");

            customer_dialog.dialog("open");
        },
        save: function () {
            var id = $("input[name='id']").val();
            var url;
            if (!id) {
                url = "/customer/save";
            } else {

                url = "/customer/edit"
            }
            customer_form.form("submit", {
                url: url,
                success: function (data) {
                    //使用easyui的form提交,需要把data转成json对象
                    data = $.parseJSON(data);
                    if (data.success) {
                        $.messager.alert('温馨提示', data.message, 'info', function () {
                            //重新加载数据表格
                            customer_datagrid.datagrid("reload");
                            //关闭弹出框
                            customer_dialog.dialog("close");

                        });
                    } else {
                        $.messager.alert('温馨提示', data.message, 'info');
                    }
                }
            })
        },

        cancel: function () {
            customer_dialog.dialog("close");
            transfer_dialog.dialog("close");
            lost_dialog.dialog("close");
        },
        //客户移交
        transfer:function () {
            transfer_form.form("clear");
            //判断选中数据
            var row = customer_datagrid.datagrid("getSelected");
            if (!row) {
                $.messager.alert('温馨提示', '请选中一条数据！', 'info');
                return;
            }

            if (row.name){
                row["cust.id"]=row.id;
            }

            if (row.chargeUser){
                row["oldSeller.id"]=row.chargeUser.id;
            }
            transfer_form.form("load", row);
            transfer_dialog.dialog("open");
        },
        //客户移交确定
        ok: function () {
            transfer_form.form("submit",{
                url:'/customerTransfer/trans',

                success:function (data) {
                    //使用easyui的form提交,需要把data转成json对象
                    data = $.parseJSON(data);
                    if (data.success){
                        $.messager.alert("温馨提示",data.message,'info',function () {
                            customer_datagrid.datagrid("load");
                            transfer_dialog.dialog("close");
                        });

                    } else {
                        $.messager.alert('温馨提示',data.messager,'info');
                    }
                }
            })
        },
        //客户流失
        lost:function () {
            lost_form.form("clear");
            //选中数据
            var row = customer_datagrid.datagrid("getSelected");
            if (!row) {
                $.messager.alert('温馨提示', '请选中一条数据！', 'info');
                return;
            }

            $.messager.confirm("温馨提示", "确定要将客户 : "+"<span style='color: red'>"+row.name+"</span>"+" 流失吗?",function (yes) {
                if(yes){

                    //发送ajax请求
                    $.get("/customer/lost?id=" + row.id, function (data) {

                        if (data.success) {

                            $.messager.alert("温馨提示", data.message, "info", function () {

                                if (row.name){
                                    row["cust.id"]=row.id;
                                }
                                if (row.chargeUser){
                                    row["chargeUser.id"]=row.chargeUser.id;
                                }

                                lost_form.form("load", row);

                                lost_dialog.dialog("open");

                            })

                        }else{
                            $.messager.alert("温馨提示",data.message,"error");
                        }
                    });
                }
            })
        },
        //客户流失表单提交
        failok: function () {
            lost_form.form("submit",{
                url:'/customerLost/lost',

                success:function (data) {
                    //使用easyui的form提交,需要把data转成json对象
                    data = $.parseJSON(data);
                    if (data.success){
                        $.messager.alert("温馨提示",data.message,'info',function () {
                            customer_datagrid.datagrid("load");
                            lost_dialog.dialog("close");
                        });
                    } else {
                        $.messager.alert('温馨提示',data.messager,'info');
                    }
                }
            })
        },

        //移入资源池
        share:function () {
            //判断选中数据
            var row = customer_datagrid.datagrid("getSelected");
            if (!row) {
                $.messager.alert('温馨提示', '请选中一条数据！', 'info');
                return;
            }
            $.messager.confirm("温馨提示", "确定要将客户 : "+"<span style='color: red'>"+row.name+"</span>"+" 移入客户资源池吗?",function (yes) {
                if (yes){
                    //移入资源池:
                    $.get("/customer/share",{id:row.id},function (data) {
                        if (data.success) {
                            $.messager.alert('温馨提示', data.message, 'info', function () {

                                customer_datagrid.datagrid("reload");
                                customer_dialog.dialog("close");

                            });
                        } else {
                            $.messager.alert('温馨提示', data.message, 'info');
                        }
                    })
                }
            } )
        },

        //升为正式客户
        victory:function () {
            //判断选中数据
            var row = customer_datagrid.datagrid("getSelected");
            if (!row) {
                $.messager.alert('温馨提示', '请选中一条数据！', 'info');
                return;
            }
            $.messager.confirm("温馨提示", "确定要将 : "+"<span style='color: red'>"+row.name+"</span>"+" 升为正式客户吗?",function (yes) {
                if (yes){
                    //升为正式客户:
                    $.get("/customer/victory",{id:row.id},function (data) {
                        if (data.success) {
                            $.messager.alert('温馨提示', data.message, 'info', function () {

                                customer_datagrid.datagrid("reload");
                                customer_dialog.dialog("close");

                            });
                        } else {
                            $.messager.alert('温馨提示', data.message, 'info');
                        }
                    })
                }
            } )
        },
        reload:function () {
            customer_datagrid.datagrid("reload");
        }
    }

    //统一绑定点击事件
    $("a[data-cmd]").click(function () {
        var methodName = $(this).data("cmd");
        methodObj[methodName]();
    })


    //初始化数据表格
    customer_datagrid.datagrid({
        fit: true,
        fitColumns: true,
        striped: true,
        toolbar: '#customer_toolbar',
        url:"/customer/list?status=1",
        pagination: true,
        singleSelect: true,
        rownumbers:true,
        columns: [[
            {field:'inputDate',title:'创建日期',width:100},

            {field:'inputUser',title:'创建人',width:60,formatter:function (value,row,index) {
                return value ? value.realName : '';
            }},
            {field:'chargeUser',title:'负责人',width:60,formatter:function (value,row,index) {
                return value ? value.realName : '';
            }},
            {field:'name',title:'客户姓名',width:70},
            {field:'age',title:'年龄',width:35},
            {field:'gender',title:'性别',width:35,formatter:function (value, row, index) {
                if (value){
                    return '男';
                }
                if (!value){
                    return '女';
                }
            }},
            {field:'job',title:'职业',width:100,formatter:function (value, row, index) {
                return value ? value.name : '';
            }},
            {field:'salaryLevel',title:'薪资水平',width:100,formatter:function (value, row, index) {
                return value ? value.name : '';
            }},
            {field:'tel',title:'电话',width:100},
            {field:'email',title:'邮箱',width:100},
            {field:'address',title:'客户地址',width:100},
            {field:'source',title:'客户来源',width:100,formatter:function (value, row, index) {
                return value ? value.name : '';
            }},
            {field:'qq',title:'QQ',width:100,align:'center',formatter:function (value) {
                return "<a target=\"_blank\"  href=\"http://wpa.qq.com/msgrd?v=3&uin="+value+"&site=qq&menu=yes\">\n" +
                    "<img border=\"0\" src=\"/static/images/qq/qq.png\" align='right' title=\"点击这里给我发消息\"/>"
                    +'<font color="#a52a2a">'+value+'</font>'+"</a>";

            },styler: function(value,row,index){
                return 'color:green;';
            }},
            {field:'wechat',title:'微信',width:100},
            {field:'idNo',title:'身份证号',width:100},

        ]],
    })

    //更新或编辑弹窗
    customer_dialog.dialog({
        width: 350,
        height: 530,
        buttons: '#customer_btns',
        closed: true
    })
    //移交客户弹出框
    transfer_dialog.dialog({
        title:'客户移交',
        width: 280,
        height: 380,
        buttons:'#transfer_btns',
        closed: true
    })
    //流失弹出框
    lost_dialog.dialog({
        title:'设置客户流失',
        width: 280,
        height: 280,
        buttons:'#lost_btns',
        closed: true
    })

})

function mysearch() {
    var chargeUser_name = $("input[name=chargeUser_name]").val();
    var beginDate = $("input[name=beginDate]").val();
    var endDate = $("input[name=endDate]").val();
    var keyword = $("input[name=keyword]").val();

    $("#customer_datagrid").datagrid("load",{
        keyword:keyword,
        chargeUser_name:chargeUser_name,
        beginDate:beginDate,
        endDate:endDate,
    })
}